<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    var car = {
      name: "宝马",
      color: "红色",
      price: 180
    }
    var temp = 0;
    Object.defineProperty(car, "price", {
      get: function () {  //get===value
        return temp
      },
      set: function (val) { //拦截给当前对象的属性赋值
        console.log(val)
        // temp = val + 20;
        if (val > 400) {
          temp = 400;
        } else {
          temp = val + 20;
        }
      },
      configurable: false,
      enumerable: false
    })
    console.log(car)

    car.price = 300;
    console.log(car)



  </script>
</body>

</html>